package com.xmb.wechat.view.wechat.setting;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.nil.sdk.utils.ACache;
import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.entity.WechatAppInfoBean;
import com.xmb.wechat.picasso.PicassoRoundByPercentTransform;
import com.xmb.wechat.util.PicChooseUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class WechatMyInfoActivity extends BaseActivity {

    @BindView(R2.id.iv_back)
    ImageView      ivBack;
    @BindView(R2.id.iv_more)
    ImageView      ivMore;
    @BindView(R2.id.iv_avatar)
    ImageView      ivAvatar;
    @BindView(R2.id.iv_arrow)
    ImageView      ivArrow;
    @BindView(R2.id.btn_pick_avatar)
    RelativeLayout btnPickAvatar;
    @BindView(R2.id.tv_name)
    TextView       tvName;
    @BindView(R2.id.et_name)
    EditText       etName;
    @BindView(R2.id.tv_id)
    TextView       tvId;
    @BindView(R2.id.et_id)
    EditText       etId;
    @BindView(R2.id.btn_save)
    Button         btnSave;

    private WechatAppInfoBean vo;
    private int               mType;

    public WechatMyInfoActivity() {
        super(R.layout.wechat_activity_my_info);
    }

    public static void start(Activity activity, int currentPosition) {
        Intent intent = new Intent(activity, WechatMyInfoActivity.class);
        intent.putExtra("data", currentPosition);
        activity.startActivity(intent);
    }

    @Override
    protected void initView() {
        mType = getIntent().getIntExtra("data", 0);
        showView(mType);
    }

    private void showView(int type) {
        switch (type) {
            case 1:
                tvId.setText("支付宝账号");
                etId.setHint("请输入支付宝账号");
                ivMore.setImageResource(R.drawable.icon_qq_switch);
                vo = WechatAppInfoBean.getAliPay(this);
                break;
            case 2:
                tvId.setText("QQ账号");
                etId.setHint("请输入QQ账号");
                ivMore.setImageResource(R.drawable.icon_alipay_switch);
                vo = WechatAppInfoBean.getQQ(this);
                break;
            default:
                tvId.setText("微信账号");
                etId.setHint("请输入微信账号");
                ivMore.setVisibility(View.GONE);
                vo = WechatAppInfoBean.getWechat(this);
                break;
        }
        Picasso.get().load("file:" + vo.getAvatar()).error(R.drawable.ic_wx_default_avatar).transform(new PicassoRoundByPercentTransform(12)).into(ivAvatar);
        etName.setText(vo.getName());
        etId.setText(vo.getWechat_id());
    }

    @OnClick({R2.id.view_placeholder, R2.id.iv_more, R2.id.btn_pick_avatar, R2.id.btn_save})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.view_placeholder) {
            finish();
        } else if (i == R.id.iv_more) {
            if (mType == 1)
                mType = 2;
            else
                mType = 1;
            showView(mType);
        } else if (i == R.id.btn_pick_avatar) {
            PicChooseUtils.chooseOnePicWithCut(WechatMyInfoActivity.this, 1, 1, Pic -> {
                vo.setAvatar(Pic[0]);
                ivAvatar.setImageBitmap(BitmapFactory.decodeFile(Pic[0]));
            });
        } else if (i == R.id.btn_save) {
            save2DB();
        }
    }

    private void save2DB() {
        if (TextUtils.isEmpty(vo.getAvatar())) {
            ToastUtils.showLong("请选择头像！");
            return;
        }
        if (TextUtils.isEmpty(etName.getText().toString().trim())) {
            ToastUtils.showLong("昵称不能为空！");
            return;
        }
        if (TextUtils.isEmpty(etId.getText().toString().trim())) {
            ToastUtils.showLong((mType == 0 ? "微信账号" : mType == 1 ? "支付宝账号" : "QQ账号") + "不能为空！");
            return;
        }
        vo.setName(etName.getText().toString());
        vo.setWechat_id(etId.getText().toString());
        ACache.get(this).put(mType == 0 ? "WechatAppInfoBean" : mType == 1 ? "AliPayAppInfoBean" : "QQAppInfoBean", vo);
        ToastUtils.showLong("保存成功");
        finish();
    }
}
